home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / SAT 2.3a1 / HeartQuest sample ƒ / scores.p < prev    next >
Encoding:
Text File  |  1994-11-19  |  12.8 KB  |  474 lines  |  [TEXT/PJMM]

  1. {================================================}
  2. {============= Score handling and display ==============}
  3. {================================================}
  4.  
  5. { Example file for Ingemars Sprite Animation Toolkit. }
  6. { © Ingemar Ragnemalm 1992 }
  7. { See doc files for legal terms for using this code. }
  8.  
  9. { This file manages the display and update of the game scores for HeartQuest.}
  10. { It holds routines for updating high score list, including asking for the name of}
  11. { the player, high score window etc. When making a new game, you will probably}
  12. { need to rewrite this unit a lot. }
  13.  
  14. unit scores;
  15.  
  16. interface
  17.     uses
  18. {$IFC UNDEFINED THINK_PASCAL}
  19.         Types, Quickdraw, 
  20. {$ENDC}
  21.         transskel, SAT, Preferences, GameGlobals, SoundConst, CenterStuff;
  22.  
  23.     var
  24.         score: longint;
  25.  
  26.     procedure DoHighMenu (item: integer);
  27.     procedure InitScores;                    { Loads the high score list and the high score window. }
  28.     procedure ZeroScore;                     { Call this on New Game! }
  29.     procedure AddScore (amount: longint);    { Call this when the player gets points, or with addscore(0) just to redisplay. }
  30.     procedure AddScoreS (amount: longint);    { Call this to redisplay when the animation isn't running. }
  31.     procedure UpdateHigh;                    { Call this on game over! }
  32.  
  33. implementation
  34.  
  35. { Highscore record }
  36.     type
  37.         hsRec = record
  38.                 HighScores: array[0..10] of longint;
  39.                 HighPlayer: array[0..10] of str15;
  40.             end;
  41.         hsPtr = ^hsRec;
  42.         hsHnd = ^hsPtr;
  43.  
  44.  
  45.     var
  46.         hs, hsm: hsHnd; { m is for macho mode }
  47.         hsh, hshm: Handle;
  48.  
  49. { Bitmaps and rects for drawhex }
  50. {    srcr, erasr: Rect;}
  51. {    bm: BitMap;}
  52. {    icon: Handle;}
  53. {    bma: array[0..9] of BitMap;}
  54. {    icons: array[0..9] of Handle;}
  55.  
  56. {Filter function for AskHigh, ok = 1 and cancel = 4}
  57.     function Filter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: integer): boolean;
  58.         var
  59.             theChar: Char;
  60.             kind: integer;
  61.             item: Handle;
  62.             box: Rect;
  63.     begin
  64.         if theEvent.what = keyDown then
  65.             begin
  66.                 theChar := Char(BitAnd(theEvent.message, charCodeMask));
  67. {if BitAnd(theEvent.modifiers, cmdkey) <> 0 then}
  68. {if theChar = '.' then}
  69.                 if ((BitAnd(theEvent.modifiers, cmdkey) <> 0) and (theChar = '.')) or (theChar = char(27)) then {cmd-. or ESC}
  70.                     begin
  71.                         itemHit := 4;
  72. {Highlight the cancel button}
  73.                         GetDItem(theDialog, 4, kind, item, box);
  74.                         HiliteControl(ControlHandle(item), 1);
  75.  
  76.                         Filter := true;
  77.                         exit(Filter);
  78.                     end;
  79.                 if (theChar = char(13)) or (theChar = char(3)) then
  80.                     begin
  81.                         itemHit := 1;
  82. {Highlight the OK button}
  83.                         GetDItem(theDialog, 1, kind, item, box);
  84.                         HiliteControl(ControlHandle(item), 1);
  85.  
  86.                         Filter := true;
  87.                         exit(Filter);
  88.                     end;
  89.             end;
  90.         Filter := false;
  91.     end;
  92.  
  93. {Put a frame around a dialog item. There are better ways to do this, though. The right way}
  94. {is to draw the frame as response to an update event, not just when opening the dialog.}
  95.     procedure FrameDItem (dLog: DialogPtr; iNum: integer);
  96.         var
  97.             iBox: Rect;
  98.             iType: integer;
  99.             iHandle: Handle;
  100.             oldPenState: PenState;
  101.             tmpp: GrafPtr;
  102.     begin
  103.         GetPort(tmpp);
  104.         SetPort(dLog);
  105.         GetPenState(oldPenState);
  106.         GetDItem(dLog, iNum, iType, iHandle, iBox);
  107.         InsetRect(iBox, -4, -4);
  108.         PenSize(3, 3);
  109.         FrameRoundRect(iBox, 16, 16);
  110.         SetPenState(oldPenState);
  111.         SetPort(tmpp);
  112.     end;
  113.  
  114. { Ask for players name (at highscore) }
  115.     function AskHigh: str255;
  116.         var
  117.             dialog: DialogPtr;
  118.             oldPort: GrafPtr;
  119.             dRec: DialogRecord;
  120.             itemHit: integer;
  121.             itemHandle: Handle;
  122.             itemType, item: integer;
  123.             itemRect: Rect;
  124.             str: str255;
  125.             levelstr: str255;
  126.     begin
  127.         CenterDialog(highDlog);
  128.         GetPort(oldPort);
  129.         dialog := GetNewDialog(highDlog, @dRec, WindowPtr(-1));
  130.         ShowWindow(dialog);
  131.         SelectWindow(dialog);
  132.         SetPort(dialog);
  133.  
  134.         GetDItem(dialog, 3, itemType, itemHandle, itemRect);
  135.         SetIText(itemHandle, features^^.player);
  136.         SelIText(dialog, 3, 0, 32767);
  137.         FrameDItem(dialog, 1);
  138.         itemHit := -1;
  139.         while (itemHit <> 1) and (itemHit <> 4) do { 1=ok, 4=cancel }
  140.             ModalDialog(@Filter, itemHit);
  141.         if itemHit = 4 then
  142.             begin
  143.                 AskHigh := '';
  144.             end;
  145.         if itemHit = 1 then
  146.             begin
  147.                 GetDItem(dialog, 3, itemType, itemHandle, itemRect);
  148.                 GetIText(itemHandle, str);
  149.                 if length(str) > 15 then
  150.                     str := Copy(str, 1, 15);
  151.                 features^^.player := str;
  152.                 AskHigh := str;
  153.             end;
  154.         CloseDialog(dialog);
  155.         SetPort(oldPort);
  156.     end;
  157.  
  158. {     High Score window handlers }
  159.  
  160.     procedure HighUpdate (resized: boolean);
  161.         var
  162.             s: str255;
  163.             i: integer;
  164.     begin
  165.         EraseRect(theHigh^.portrect);
  166.         TextSize(9);
  167.  
  168.         moveto(10, 20);
  169.         DrawString(MyGetIndString(normalStrID)); {str 9: Normal high score list}
  170.         MoveTo(150, 20);
  171.         DrawString(MyGetIndString(machoStrID)); {str 10: Macho high score list}
  172.         MoveTo(0, 22);
  173.         LineTo(500, 22);
  174.         MoveTo(140, 0);
  175.         LineTo(140, 400);
  176.  
  177.         for i := 1 to 10 do
  178.             begin
  179.                 if not LastMacho and (i = LastHigh) then
  180.                     begin
  181.                         TextFace([bold]);
  182.                         ForeColor(redColor);
  183.                     end;
  184.                 moveto(10, i * 18 + 20);
  185.                 DrawString(hs^^.HighPlayer[i]);
  186.                 moveto(110, i * 18 + 20);
  187.                 NumToString(hs^^.HighScores[i], s);
  188.                 DrawString(s);
  189.  
  190.                 TextFace([]);
  191.                 ForeColor(BlackColor);
  192.                 if LastMacho and (i = LastHigh) then
  193.                     begin
  194.                         TextFace([bold]);
  195.                         ForeColor(redColor);
  196.                     end;
  197.                 moveto(150, i * 18 + 20);
  198.                 DrawString(hsm^^.HighPlayer[i]);
  199.                 moveto(250, i * 18 + 20);
  200.                 NumToString(hsm^^.HighScores[i], s);
  201.                 DrawString(s);
  202.  
  203.                 TextFace([]);
  204.                 ForeColor(BlackColor);
  205.             end;
  206.         TextSize(12);
  207.     end;
  208.  
  209.     procedure HighHalt;
  210.     begin
  211.         CloseWindow(theHigh);
  212.     end;
  213.  
  214.     function InternalAddScore (amount: longint): Rect;
  215.         var
  216.             s: str255;
  217.             r: Rect;
  218.     begin
  219.         score := score + amount;
  220.  
  221.         SetPort(gSAT.backScreen);
  222.         SetRect(r, gSAT.offSizeH - 49, 14, gSAT.offSizeH - 2, 155);
  223.         EraseRoundRect(r, 10, 10);
  224.         FrameRoundRect(r, 10, 10);
  225.         NumToString(Score, s);
  226.         MoveTo(gSAT.offSizeH - 47, 30);
  227.         DrawString(MyGetIndString(scoreStrID)); {str 11: Score: }
  228.         MoveTo(gSAT.offSizeH - 47, 50);
  229.         DrawString(s);
  230.  
  231.         if not bonusLevelRunning then
  232.             begin
  233.                 NumToString(bonus, s);
  234.                 MoveTo(gSAT.offSizeH - 47, 80);
  235.                 DrawString(MyGetIndString(bonusStrID)); {str 12: Bonus: }
  236.                 MoveTo(gSAT.offSizeH - 47, 100);
  237.                 DrawString(s);
  238.             end;
  239.  
  240.         NumToString(level, s);
  241.         MoveTo(gSAT.offSizeH - 47, 130);
  242.         DrawString(MyGetIndString(levelStrID)); {str 13: Level: }
  243.         MoveTo(gSAT.offSizeH - 47, 150);
  244.         DrawString(s);
  245.         InternalAddScore := r;
  246.     end;
  247.  
  248.     procedure AddScore (amount: longint);
  249.         var
  250.             s: str255;
  251.             r: Rect;
  252.             tmpport: grafptr;
  253.     begin
  254.         GetPort(tmpPort);
  255.         r := InternalAddScore(amount);
  256.         SATBackChanged(r); {Let SAT show it on screen}
  257.         SetPort(tmpPort);
  258.     end;
  259.  
  260.     procedure AddScoreS (amount: longint);
  261.         var
  262.             s: str255;
  263.             r: Rect;
  264.             tmpport: grafptr;
  265.     begin
  266.         GetPort(tmpPort);
  267.         r := InternalAddScore(amount);
  268.         CopyBits(gSAT.backScreen^.portbits, gSAT.wind^.portBits, r, r, srcCopy, nil);
  269.         CopyBits(gSAT.backScreen^.portbits, gSAT.offScreen^.portBits, r, r, srcCopy, nil);
  270.         SetPort(tmpPort);
  271.     end;
  272.  
  273.     procedure DoHighMenu (item: integer);
  274.         var
  275.             p: procptr;
  276.             i: integer;
  277.     begin
  278.         case item of
  279.             showhs: 
  280.                 begin
  281.                     ShowWindow(theHigh);
  282.                     SelectWindow(theHigh);
  283.                 end;
  284.             clearhs: 
  285.                 begin
  286.                     if QuestionStr(MyGetIndString(sureStrID)) then {str 14: Are you sure you want to erase the high scores?}
  287.                         begin
  288.                             for i := 1 to 10 do
  289.                                 begin
  290.                                     hs^^.HighScores[i] := 0;            { skall läsas från fil eller resurs }
  291.                                     hs^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15: Nobody}
  292.                                     hsm^^.HighScores[i] := 0;            { skall läsas från fil eller resurs }
  293.                                     hsm^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15}
  294.                                 end;
  295.                             hs^^.HighScores[0] := 10000;            { Lowscore }
  296.                             hsm^^.HighScores[0] := 10000;            { Lowscore }
  297.                             ChangedResource(handle(hs));
  298.                             ChangedResource(handle(hsm));
  299.                             HideWindow(theHigh);
  300.                         end;
  301.                 end;
  302.             otherwise
  303.                 ;
  304.         end;
  305.     end;
  306.  
  307.     procedure WindKey (theChar: char; theMods: integer);
  308.     begin
  309.     end;
  310.  
  311. { Call this on game over! }
  312.     procedure UpdateHigh;
  313.         var
  314.             num, len: integer;
  315.             name, s: str255;
  316.     begin
  317.         lastMacho := features^^.macho;
  318.  
  319.         if features^^.macho then
  320.             begin
  321.                 if score > hsm^^.HighScores[10] then
  322.                     begin
  323.                         num := 10;
  324.                         name := AskHigh;
  325.                         NumToString(level, s); {used below, to append level number}
  326. {Max 15 characters! We take some extra trouble to append '…' too.}
  327.                         len := length(stringof(' (', s, ')'));
  328.                         if length(name) > 15 - len then
  329.                             name := Concat(Copy(name, 1, 15 - len - 1), '…');
  330.  
  331.                         if name = '' then { alt length(name) = 0 }
  332.                             exit(updatehigh);
  333.                         while (hsm^^.HighScores[num - 1] < score) and (num > 1) do
  334.                             begin
  335.                                 hsm^^.HighScores[num] := hsm^^.HighScores[num - 1];
  336.                                 hsm^^.HighPlayer[num] := hsm^^.HighPlayer[num - 1];
  337.                                 num := num - 1;
  338.                             end;
  339.                         LastHigh := num; {Remember last high for the highscore display}
  340.                         hsm^^.HighScores[num] := score;
  341.                         hsm^^.HighPlayer[num] := stringof(name, ' (', s, ')'); {AskHigh;}
  342.                         ChangedResource(handle(hsm));
  343.                         HideWindow(theHigh);
  344.                         ShowWindow(theHigh);
  345.                         SelectWindow(theHigh);
  346.                     end;
  347.             end{ if macho }
  348.         else if score > hs^^.HighScores[10] then
  349.             begin
  350.                 num := 10;
  351.                 name := AskHigh;
  352.                 if length(name) > 15 then
  353.                     name := Concat(Copy(name, 1, 14), '…');
  354.  
  355.                 if name = '' then { alt length(name) = 0 }
  356.                     exit(updatehigh);
  357.                 while (hs^^.HighScores[num - 1] < score) and (num > 1) do
  358.                     begin
  359.                         hs^^.HighScores[num] := hs^^.HighScores[num - 1];
  360.                         hs^^.HighPlayer[num] := hs^^.HighPlayer[num - 1];
  361.                         num := num - 1;
  362.                     end;
  363.                 LastHigh := num; {Remember last high for the highscore display}
  364.                 hs^^.HighScores[num] := score;
  365.                 hs^^.HighPlayer[num] := name;
  366.                 ChangedResource(handle(hs));
  367.                 HideWindow(theHigh);
  368.                 ShowWindow(theHigh);
  369.                 SelectWindow(theHigh);
  370.             end;
  371.     end;
  372.  
  373.     procedure ZeroScore;
  374.     begin
  375.         score := 0;
  376.         LastHigh := -1;
  377.     end;
  378.  
  379. {This procedure copies a resource from the file applFile to prefFile (global file numbers,}
  380. {from the unit Preferences).}
  381. {OBSOLETE - should be replaced by the better code in Preferences.p!}
  382.     procedure OldCopyResource (resType: OSType; id: integer);
  383.         var
  384.             h, h2: Handle;
  385.             saveFile: integer;
  386.     begin
  387.         saveFile := CurResFile; {Look where we are so we can restore}
  388.         UseResFile(gAppFile);
  389.  
  390.         h := GetResource(resType, id); {Get res from the appl}
  391.         if h <> nil then
  392.             begin
  393.                 UseResFile(gPrefFile);
  394.                 h2 := GetResource(resType, id);
  395.                 if h2 = nil then {It doesn't already exist}
  396.                     begin
  397.                         DetachResource(h); {Detach it so we can move it.}
  398.                         AddResource(h, resType, id, ''); {Put it into the gPrefFile}
  399.                         ReleaseResource(h);
  400.                     end
  401.                 else {The res always exists. Don't copy.}
  402.                     begin
  403.                         ReleaseResource(h);
  404.                         ReleaseResource(h2);
  405.                     end;
  406.             end;
  407.         UseResFile(saveFile); {restore}
  408.     end;
  409.  
  410.     procedure InitScores;
  411.         var
  412.             i: integer;
  413.             ignoreErr: OSErr;
  414.     begin
  415.         if SetPrefFile(kPrefsFileName, kPrefCreator, kPrefType, false) then {If a pref file was created, copy high scores to it!}
  416.             begin
  417. {CopyResource('Bäst', 0); {Normal mode high scores}
  418. {CopyResource('Bäst', 1); {Macho mode high scores}
  419. {CopyResource('Feat', 0); {Settings}
  420.                 ignoreErr := CopyResource(gAppFile, gPrefFile, 'Bäst', 0); {Normal mode high scores}
  421.                 ignoreErr := CopyResource(gAppFile, gPrefFile, 'Bäst', 1); {Macho mode high scores}
  422.                 ignoreErr := CopyResource(gAppFile, gPrefFile, 'Feat', 0); {Settings}
  423.             end
  424.         else
  425.             gPrefFile := gAppFile; {If we have no pref file, let's make sure we UseResFile to something that exists.}
  426.  
  427.         lastHigh := -1; {no "last"}
  428.  
  429.         theHigh := GetNewWindow(theHighRes, nil, WindowPtr(-1));
  430.         SetPort(theHigh);
  431.         dummy := SkelWindow(theHigh, nil, @WindKey, @HighUpdate, nil, nil, @HighHalt, nil, false);
  432.  
  433.         UseResFile(gPrefFile); {set the resfile to the pref file, if any. If none, gPrefFile will be the app itself!}
  434.  
  435.         hs := hsHnd(GetResource('Bäst', 0));
  436.         if hs = nil then {Didn't exist - create it!}
  437.             begin
  438.                 hs := hsHnd(NewHandle(Sizeof(hsRec)));
  439.                 CheckNoMem(Ptr(hs));
  440.                 for i := 1 to 10 do
  441.                     begin
  442.                         hs^^.HighScores[i] := 0;
  443.                         hs^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15}
  444.                     end;
  445.                 hs^^.HighScores[0] := 10000;            { Lowscore }
  446.                 AddResource(handle(hs), 'Bäst', 0, 'High scores');
  447.             end
  448.         else {Did exist - check the size!}
  449.             if GetHandleSize(Handle(hs)) < sizeof(hsHnd) then
  450.                 SetHandleSize(Handle(hs), sizeof(hsHnd));
  451.  
  452.         hsm := hsHnd(GetResource('Bäst', 1));
  453.         if hsm = nil then {Didn't exist - create it!}
  454.             begin
  455.                 hsm := hsHnd(NewHandle(Sizeof(hsRec)));
  456.                 CheckNoMem(Ptr(hsm));
  457.                 for i := 1 to 10 do
  458.                     begin
  459.                         hsm^^.HighScores[i] := 0;            { skall läsas från fil eller resurs }
  460.                         hsm^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15}
  461.                     end;
  462.                 hsm^^.HighScores[0] := 10000;            { Lowscore }
  463.                 AddResource(handle(hsm), 'Bäst', 1, 'High scores');
  464.             end
  465.         else {Did exist - check the size!}
  466.             if GetHandleSize(Handle(hsm)) < sizeof(hsHnd) then
  467.                 SetHandleSize(Handle(hsm), sizeof(hsHnd));
  468.  
  469.         UseResFile(gAppFile);
  470.  
  471.         score := 0;
  472.     end;
  473.  
  474. end.